package com.lewis.xuanke.controller;

import com.lewis.xuanke.entity.ComResult;
import com.lewis.xuanke.entity.Course;
import com.lewis.xuanke.entity.Record;
import com.lewis.xuanke.entity.User;
import com.lewis.xuanke.mapper.CourseMapper;
import com.lewis.xuanke.mapper.RecordMapper;
import com.lewis.xuanke.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/course")
public class CourseController {
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    RecordMapper recordMapper;

    //查询课程
    @RequestMapping(value = "/getMyCourse", method = RequestMethod.POST)
    public ComResult<List<Course>> getMyCourse(int pid) {

        List<Course> list = courseMapper.selectAll();
        List<Course> courses = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Course course = list.get(i);
            if (course.getPid() == pid) {
                courses.add(course);
            }
        }
        ComResult<List<Course>> result = new ComResult<>();

        result.setMsg("sucess");

        result.setCode(0);
        result.setData(courses);
        return result;
    }

    //查询课程
    @RequestMapping(value = "/getUserByCid", method = RequestMethod.POST)
    public ComResult<List<User>> getCourseByWeek(Integer cid) {
        Course course = courseMapper.selectByPrimaryKey(cid);
        List<User> users = userMapper.selectAll();
        List<User> userList = new ArrayList<>();
        for (User user : users) {
            if (course != null && course.getZhuanye().equals(user.getZhuanye()) && course.getXueyuan().equals(user.getXueyuan()) && course.getBanji().equals(user.getBanji())) {
                Record record = new Record();
                record.setUid(user.getId());
                record.setCid(cid);
                Record record1 = recordMapper.getRecord(record);
                if (record1 != null) {
                    user.setQ(true);
                } else {
                    user.setQ(false);
                }
                userList.add(user);
            }
        }

        ComResult<List<User>> result = new ComResult<>();

        result.setMsg("success");

        result.setCode(0);
        result.setData(userList);
        return result;
    }

    //发布课程
    @RequestMapping(value = "/pubCourse", method = RequestMethod.POST)
    public ComResult pubKc(Integer pid, String xueyuan, String banji, String zhuanye, String kemu, String erweima) {
        Course course = new Course();
        course.setBanji(banji);
        course.setCtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        course.setErweima(erweima);
        course.setPid(pid);
        course.setXueyuan(xueyuan);
        course.setZhuanye(zhuanye);
        course.setKemu(kemu);
        int i = courseMapper.insert(course);
        ComResult result = new ComResult<>();

        result.setData(course.getId());
        if (i == 1) {
            result.setMsg("创建成功");
        } else {
            result.setMsg("失败");
        }
        result.setCode(0);
        return result;
    }

    //发布课程
    @RequestMapping(value = "/pubRecord", method = RequestMethod.POST)
    public ComResult pubRecord(Integer cid, Integer uid, String time) {
        Record course = new Record();
        course.setCtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        course.setCid(cid);
        course.setUid(uid);
        int i = recordMapper.insert(course);
        ComResult result = new ComResult<>();
        if (i == 1) {
            result.setMsg("签到成功" + time);
        } else {
            result.setMsg("失败");
        }
        result.setCode(0);
        return result;
    }


    //删除课程
    @RequestMapping(value = "/delCourse", method = RequestMethod.POST)
    public ComResult delCourse(Integer id) {

        int i = courseMapper.deleteByPrimaryKey(id);
        ComResult result = new ComResult<>();


        if (i == 1) {
            result.setMsg("删除成功");
        } else {
            result.setMsg("删除失败");
        }
        result.setCode(0);
        return result;
    }


}
